import { fetch } from '../services/api/photos'

export default {
  namespace: 'photos',

  state: {
    page:  1,
    list: []
  },

  reducers: {
    setlist (state, action) {
      const { page, list} = action.payload
      return {
        ...state,
        page,
        list: [...state.list, ...list]
      }
    }
  },

  effects: {
    * getPhotos (_, { call, put, select}) {
      const page = yield select(state => state.photos.page)
      const res = yield call(fetch, { page })
      yield put({
        type: 'setlist',
        payload: {
          page: page + 1,
          list: res.data
        }
      })
    }
  }
}
